"use strict";

var app = new Vue({
    el: "#app",
    mixins: [mixin_page],
    store: store,
    // router,
    data: function data() {
        return {
            url_get_obj: "1",
            timer: null,
            form_mint: {
                YASHA: 0,
                trx: 0
            },
            form_lock: {
                YASHA: 0
            },
            time: {
                hh: "00",
                mm: "00",
                ss: "00"
            },
            timer_end: null,
            start_time: "2020/11/12 21:00:00"
        };
    },
    computed: {
        longtime: function longtime() {
            var t0 = this.chain['YASHA IN WALLET'].value;
            var t1 = new Date() / 1000;
            var t = t0 - t1;

            if (t < 0) {
                return 0;
            }

            return Math.ceil(t / 3600 / 24 * 10) / 10;
        },
        locked_YASHA: function locked_YASHA() {
            return Math.round(this.chain["LOCKED YASHA"].value / this.$tron.config.precision * 10000) / 10000;
        },
        address: function address() {
            return this.$store.state.user.address;
        },
        trx_rate: function trx_rate() {
            var arr = [0, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380, 400, 420, 440, 460, 480,
                500
            ];
            var level = this.chain["YASHA PRICE LEVEL"].value || 0;
            return arr[level];

        },
        next_price: function next_price() {
            console.log(this.chain["NEXT PRICE"].value);
            return Math.floor(this.chain["NEXT PRICE"].value * 10000) / 10000;
        },
        count_YASHA: function count_YASHA() {
            console.log(this.chain["COUNT YASHA"].value);
            return Math.round(this.chain["COUNT YASHA"].value / this.$tron.config.precision * 10000) / 10000;
        },
        minted_YASHA: function minted_YASHA() {
            return Math.floor(this.chain["MINTED YASHA"].value / this.$tron.config.precision * 10000) / 10000;
        },
        surplus: function surplus() {
            return this.count_YASHA - this.minted_YASHA % this.count_YASHA;
        },
        isOpen: function isOpen() {
            var hh = this.time.hh;
            var mm = this.time.mm;
            var ss = this.time.ss;
            if (hh <= 0 && mm <= 0 && ss <= 0) {
                return true;
            } else {
                return false
            }
        }
    },
    methods: {
        run_start: function () {
            var time = new Date(this.start_time);
            var now = new Date();
            if (now < time) {
                var _this = this;
                this.timer_end = setInterval(function () {
                    var date = new Date();
                    var stamp = (time - date) / 1000;

                    var hh = parseInt(stamp / 60 / 60 % 24);
                    var mm = parseInt(stamp / 60 % 60);
                    var ss = parseInt(stamp % 60);
                    if (hh <= 0 && mm <= 0 && ss <= 0) {
                        _this.time = {
                            hh: "00",
                            mm: "00",
                            ss: "00"
                        }
                        clearInterval(this.timer_end);
                    }
                    if (hh < 10) {
                        hh = "0" + hh;
                    }
                    if (mm < 10) {
                        mm = "0" + mm;
                    }
                    if (ss < 10) {
                        ss = "0" + ss;
                    }
                    _this.time = {
                        hh: hh,
                        mm: mm,
                        ss: ss
                    }
                }, 1000);
            }
        },
        copy() { },
        lock: async function lock() {
            var f = this.form_lock;

            if (this.user.balance < f.YASHA || this.user.balance <= 0) {
                this.$message({
                    message: "Insufficient YASHA in wallet!"
                });
                return;
            }

            var num = Math.round(Number(f.YASHA) * this.$tron.config.precision);
            // this.$tron.send('transfer', null, this.$tron.config.contract_address, num);
        },
        claim: async function claim() {
            var _this = this;

            this.$tron.send('claim').then(function (res) {
                _this.$message({
                    message: "CLAIMING...",
                    type: "success"
                });
            });
        },
        redeem: async function redeem() {
            var _this2 = this;

            if (this.chain["LOCKED YASHA"].value == 0) {
                this.$message({
                    message: "There is no YASHA to unlock"
                });
                return;
            }

            this.$tron.send('redeem').then(function (res) {
                _this2.$message({
                    message: "UNLOCKING...",
                    type: "success"
                });
            });
        },
        mint: async function mint() {
            var _this = this;

            var value = this.$tron.toChage_16(Math.round(this.form_mint.trx * Math.pow(10, 6)), 1);
            var address = localStorage.getItem('invitation_address');

            if (!address || address === "undefined") {
                address = "0x0000000000000000000000000000000000000000";
            }

            this.$tron.send('buyToken', value, value, address).then(function (txHash) {
                // if (txHash) {
                // 	_this3.$tron.req('trx_getBalance', [_this3.user.address, 'latest']).then(function(res) {
                // 		_this3.$tron.toChage_10(res);
                // 	});
                // }
            }).catch(function (error) { });
        },
        input_trx: function input_trx(e) {
            if (!e.target) {
                return
            }
            var p = Number(e.target.value);
            var price = this.surplus * this.trx_rate;
            var num = 0;

            if (p > price) {
                num += this.surplus;
                p -= price;
                num += p / this.next_price;
            } else {
                num = p / this.trx_rate;
            }

            this.form_mint.YASHA = Math.round(num * 10000) / 10000;
        },
        input_YASHA: function input_YASHA(e) {
            if (!e.target) {
                return
            }
            var n = Number(e.target.value);
            var price = 0;

            if (n > this.surplus) {
                price = this.surplus * this.trx_rate;
                n -= this.surplus;
                price += n * this.next_price;
            } else {
                price = n * this.trx_rate;
            }

            this.form_mint.trx = Math.round(price * 10000) / 10000;
        },
        login: async function login() {
            var arrdess = await this.$tron.login();
            this.get_balance();
        },
        getAll: async function getAll() {
            var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "chain";
            var list = this.$store.state.web[type];
            var dict = {};

            for (var k in list) {
                var o = list[k];

                if (o && o.method) {
                    var res;
                    if (o.param !== undefined) {
                        res = await this.$tron.call(o.method, null, o.param);
                    } else {
                        res = await this.$tron.call(o.method, null);
                    }
                    if (res) {
                        var obj = {};
                        obj[k] = Object.assign({}, o, {
                            value: res.toNumber()
                        });
                        this.$store.commit('set_' + type, obj);
                    }
                }
            }
        },
        runing_time: async function runing_time() {
            this.getAll("chain");
            this.getAll("info");
            this.get_balance();
        },
        get_obj: async function get_obj(res) {
            var _this = this;
            var timer = setInterval(async function () {
                var address = await _this.get_address();

                if (!address) {
                    return;
                }
                clearInterval(timer);
                _this.runing_time();
                _this.timer = setInterval(function () {
                    _this.runing_time();
                }, 7000);
            }, 100);
        }
    },
    mounted() {
        this.run_start()
    },
    destroyed: function destroyed() {
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = null;
        }

        if (this.timer_end) {
            clearInterval(this.timer_end);
            this.timer_end = null;
        }
    }
});